using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Threading;
using System.Diagnostics;
using System.Collections;

using System.Runtime.Serialization;
using System.Runtime.Versioning;
using System.Net.Sockets;
using BrainTechLLC;

namespace Mud.EmlenMudMain
{
    [Serializable]
    public class PlayerbaseData : IPlayerbaseData
    {

        public PlayerbaseData() { }
        private string playerName;	/* Player's name */

        public string PlayerName
        {
            get { return playerName; }
            set { playerName = value; }
        }
        private DateTime player_last_login = DateTime.MinValue;	/* Last login date-time stamp */

        public DateTime Player_last_login
        {
            get { return player_last_login; }
            set { player_last_login = value; }
        }
        private bool player_good = true;		/* Is good or evil? */

        public bool Player_good
        {
            get { return player_good; }
            set { player_good = value; }
        }
        private int player_level = 0;		/* Current player level */

        public int Player_level
        {
            get { return player_level; }
            set { player_level = value; }
        }
        private int player_nodelete = 0;	/* Nodelete flag? */

        public int Player_nodelete
        {
            get { return player_nodelete; }
            set { player_nodelete = value; }
        }
        private string email_address = "";	/* Player's email addy */

        public string Email_address
        {
            get { return email_address; }
            set { email_address = value; }
        }

        public static bool Load(int inVersion)
        {
            if (SerializePlayerbaseIn(inVersion) == false)
            {
                EmlenMud.Oldschool.load_playerbase_old_format();
                if (MiscConstants.DataStorageFormat == DataStorageFormatTypes.CompressedSerialization)
                    PlayerbaseData.Save(inVersion);
            }
            return true;
        }

        public static bool Save(int outVersion)
        {
            return SerializePlayerbaseOut(outVersion);
        }

        public static bool SerializePlayerbaseIn(int inVersion)
        {
#if NO_SILVERLIGHT
            string szFName = EmlenMud.HostPathnameToMudDirectory + "\\" + MiscConstants.DataFileDirectory + "\\" + typeof(PlayerbaseData).Name.ToString() + inVersion.ToString();
            Object o = SerializationHelper.SerializeObjectIn(szFName);
            if (o == null) return false;
            if (o is ArrayList)
            {
                ArrayList rgIn = (ArrayList)o;
                EmlenMud.WorldTemplates.rgPlayerbase.Clear();
                EmlenMud.WorldTemplates.htPlayerbase.Clear();
                foreach (PlayerbaseData pb in rgIn)
                {
                    PlayerbaseData.AddPlayer(pb);
                }
                return true;
            }
            return false;
#else 
            return true;
#endif
        }

        public static bool SerializePlayerbaseOut(int outVersion)
        {
#if NO_SILVERLIGHT
            string szFName = EmlenMud.HostPathnameToMudDirectory + "\\" + MiscConstants.DataFileDirectory + "\\" + typeof(PlayerbaseData).Name.ToString() + outVersion.ToString();
            return SerializationHelper.SerializeObjectOut(EmlenMud.WorldTemplates.rgPlayerbase, szFName, true);
#else
            return true;
#endif
        }

        public static bool AddPlayer(PlayerbaseData pb)
        {
            //if (pb.EmlenMud == null) pb.AssignEmlenMud(EmlenMud);
            if (EmlenMud.WorldTemplates.htPlayerbase[pb.playerName] == null)
            {
                EmlenMud.WorldTemplates.htPlayerbase[pb.playerName] = pb;
                EmlenMud.WorldTemplates.rgPlayerbase.Add(pb);
                return true;
            }
            return false;
        }
    }
}

// OLD CODE to PORT - actually, much of it may be unnecessary

//void reload_pbase (CharacterData  chr, string szUserArguments)
//{
//  PLAYERBASE_DATA *play;
//  PLAYERBASE_DATA *pn;
//  DEFINE_COMMAND ("z_reload_pbase", reload_pbase, POSITION_DEAD, 110, ALWAYS, "This command reloads the pbase datafile.")
//    for (play = playerbase_zero; play != NULL; play = pn)
//    {
//      pn = play->next;
//      free (play);
//    }
//  load_playerbase ();
//  return;
//}


//void p_refresh (CharacterData  chr, string szUserArguments)
//{
//  char buf[STD_LENGTH];
//  DEFINE_COMMAND ("z_refresh", p_refresh, POSITION_DEAD, 110, ALWAYS, "Resets all players' inactivity counters.")

//    sprintf (buf, "util -refresh");
//  system (buf);
//  reload_pbase (chr, szUserArguments);
//  return;
//}



//void clean_up (CharacterData  chr, string szUserArguments)
//{
//  char buf[STD_LENGTH];
//  DEFINE_COMMAND ("z_clean_pfile", clean_up, POSITION_DEAD, 110, ALWAYS, "Deletes pfiles of all people not in the pfile database!!  Dangerous.")

//    sprintf (buf, "util -cleanup &");
//  system (buf);
//  return;
//}



//void clean_oldchars (CharacterData  chr, string szUserArguments)
//{
//  char buf[STD_LENGTH];
//  DEFINE_COMMAND ("z_clean_oldchars", clean_oldchars, POSITION_DEAD, 110, ALWAYS, "Cleans up people who haven't logged in for quite a while.  Lags the mud.")

//    sprintf (buf, "util -deleteold");
//  system (buf);
//  reload_pbase (chr, szUserArguments);
//  return;
//}

//void 
//update_pbase (CharacterData  chr)
//{
//  bool ffund;
//  PLAYERBASE_DATA *player;
//  ffund = FALSE;
//  if (!str_cmp (NAME (chr), "Orin"))
//    return;
//  for (player = playerbase_zero; player != NULL; player = player->next)
//    {
//      if (player->player_level != -1 && !str_cmp (player->playerName, NAME (chr)))
//   {
//     if (chr->pcdata->level != ((player->player_level > 200) ? (player->player_level - 200) : player->player_level))
//       {
//         player->player_last_login = (int) chr->pcdata->logon;
//         if (player->email_address[0] == '\0')
//      strcpy (player->email_address, chr->pcdata->email);
//         player->player_good = !(IS_EVIL (chr));
//         player->player_level = ((chr->pcdata->nRemortTimes > 0) ? (chr->pcdata->level + 200) : chr->pcdata->level);
//         save_playerbase ();
//         return;
//       }
//     ffund = TRUE;
//     break;
//   }
//    }
//  if (ffund == FALSE)
//    {
//      bool isfree;
//      isfree = FALSE;
//      player = new_player ();
//      strcpy (player->playerName, NAME (chr));
//      player->player_last_login = (int) chr->pcdata->logon;
//      player->player_good = !(IS_EVIL (chr));
//      strcpy (player->email_address, chr->pcdata->email);
//      player->player_level = chr->pcdata->level;
//      player->player_nodelete = FALSE;
//      isfree = TRUE;
//      save_playerbase ();
//    }
//  return;
//}

//void 
//fcheck_pbase (string n)
//{
//  bool ffund;
//  PLAYERBASE_DATA *player;
//  ffund = FALSE;
//  if (!str_cmp (n, "Orin"))
//    return;
//  for (player = playerbase_zero; player != NULL; player = player->next)
//    {
//      if (!str_cmp (player->playerName, n))
//   return;
//    }
//  player = new_player ();
//  strcpy (player->playerName, n);
//  player->player_last_login = (int) currentTime;
//  player->player_good = 1;
//  strcpy (player->email_address, "not_recorded_yet");
//  player->player_level = 5;
//  player->player_nodelete = FALSE;
//  save_playerbase ();
//  return;
//}
